﻿using MediatR;
using Traceability.Commont.Domains;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.EnterpriseFiles
{
    public class EnterpriseListHander:IRequestHandler<EnterpriseListCommand, PageDTO<Enterprises>>
    {
        private readonly IBateRepository<Enterprises> enter;

        public EnterpriseListHander(IBateRepository<Enterprises> enter)
        {
            this.enter = enter;
        }

        public async Task<PageDTO<Enterprises>> Handle(EnterpriseListCommand request, CancellationToken cancellationToken)
        {
            try
            {
                PageDTO<Enterprises> res = new PageDTO<Enterprises>();
                var list = enter.GetAll().ToList();
                if (!string.IsNullOrEmpty(request.EnterpriseName))
                {
                    list = list.Where(x => x.EnterpriseName.Contains(request.EnterpriseName)).ToList();
                }
                res.totalCount = list.Count;
                res.pageCount = (int)Math.Ceiling(list.Count * 1.0 / request.pageSize);
                res.Lists = list.OrderBy(x => x.Enterpriseid).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
                return res;

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
